package com.sxm.yiwei.utils;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sxm.yiwei.tools.ErrType;

import org.xutils.common.Callback;
import org.xutils.common.util.DensityUtil;
import org.xutils.http.RequestParams;
import org.xutils.image.ImageOptions;
import org.xutils.x;

/**
 * Created by KuKa on 2017/10/26.
 * 网络请求类
 */

public class GetUiltsData {

        // get方法
    public static void xGetMethod(RequestParams params, final Context context, final CallBackUtilsInf callBackUtilsInf, final String fromWhere){
        params.setConnectTimeout(5*1000);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                JSONObject object = JSON.parseObject(result);
                String status = object.getString("status");
                if (status.equals("1")){
                    callBackUtilsInf.onSuccess(result,fromWhere);
                }else {
                    callBackUtilsInf.onSuccessError(result,fromWhere);
                    String failure = object.getString("failure");
                    ErrType.successErrCode(failure,context);

                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ErrType.errType(context,ex);
                callBackUtilsInf.onError(ex);

            }

            @Override
            public void onCancelled(CancelledException cex) {
                Toast.makeText(context, "已取消", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFinished() {

            }
        });
    }
        // 返回数据


    // post方法
    public static void xPostMethod(RequestParams params, final Context context, final CallBackUtilsInf callBackUtilsInf, final String fromWhere){
        params.setConnectTimeout(5*1000);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                JSONObject object = JSON.parseObject(result);
                String status = object.getString("status");
                if (status.equals("1")){// 请求成功
                    callBackUtilsInf.onSuccess(result,fromWhere);
                }else {// 其他错误码
                    callBackUtilsInf.onSuccessError(result,fromWhere);
                    String failure = object.getString("failure");
                    ErrType.successErrCode(failure,context);

                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ErrType.errType(context,ex);
                callBackUtilsInf.onError(ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Toast.makeText(context, "已取消", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFinished() {

            }
        });

    }


    public interface  CallBackUtilsInf{
        void onSuccess(String result,String fromWhere);
        void onError(Throwable ex);
        void onSuccessError(String result,String fromWhere);
    }


}
